# -*- conding:UTF-8 -*-

'''
@ Project: news
@ File: class.py
@ Author: 韩成瑾
@ Date：2022/7/28 21:01

'''

from flask import Blueprint, jsonify
from flask_restful import Resource, Api, reqparse
from models.model import *

course_bp = Blueprint('course_bp', __name__, url_prefix='/course')

api = Api(course_bp)




class CoursesView(Resource):
    # 获取所有课程
    def get(self):
        course_info = Course.query.all()
        course_list = []
        for course in course_info:
            a = {
                'id':course.id,
                'name':course.name,
                'info':course.info,
                'total':course.total,
                'money':float(course.money),
            }
            course_list.append(a)
        return jsonify({
            'code': 200,
            'teacher_list': course_list
        })

    # 添加课程
    def post(self):
        req = reqparse.RequestParser()
        req.add_argument('name')
        req.add_argument('info')
        req.add_argument('start_time')
        req.add_argument('end_time')
        req.add_argument('total_time')
        req.add_argument('total')
        req.add_argument('money')
        req.add_argument('type')
        args = req.parse_args()

        course = Course(name=args['name'],info=args['info'],start_time=args['start_time'],end_time=args['end_time'],
                        total=args['total'],money=args['money'],type=args['type'],total_time=args['total_time'])
        db.session.add(course)
        db.session.commit()

        return jsonify({
            'code':200,'msg':'添加成功'
        })




class CourseView(Resource):
    # 获取单个课程
    def get(self,id):
        course_one = Course.query.filter(Course.id==id).one()
        course_one_list = {
            'id':course_one.id,
            'name':course_one.name,
            'info': course_one.info,
            'total': course_one.total,
            'money': float(course_one.money),
        }
        return jsonify({
            'code':200,
            'list':course_one_list
        })

    # 修改课程
    def put(self,id):
        req = reqparse.RequestParser()
        req.add_argument('name')
        req.add_argument('info')
        req.add_argument('total')
        req.add_argument('money')
        args = req.parse_args()

        course_info = Course.query.filter(Course.id==id).first()
        if not course_info:
            return jsonify({
                'code':400,'msg':'没有这个课程'
            })
        Course.query.filter(Course.id==id).update(args)
        db.session.commit()
        return jsonify({
            'code': 200, 'msg': "修改成功"
        })

    # 删除课程
    def delete(self,id):
        req = reqparse.RequestParser()
        req.add_argument('id')
        args = req.parse_args()

        Course.query.filter(Course.id == id).delete()
        db.session.commit()

        return jsonify({
            'code': 200, 'msg': '删除成功'
        })










api.add_resource(CoursesView,'/course')
api.add_resource(CourseView,'/course/<int:id>')
